package cn.wuxun.user.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jt.common.pojo.Manager;
import com.jt.common.pojo.User;
import com.jt.common.utils.CookieUtils;
import com.jt.common.vo.SysResult;

import cn.wuxun.user.service.ManagerService;
@RestController
public class ManagerController {
	@Autowired
	private ManagerService managerService;
	/**
	 * @return 管理员后台登录
	 */
	@RequestMapping("/managerLogin")
	public SysResult managerLogin(HttpServletRequest request, HttpServletResponse response,Manager manager) {
		// 调用业务层确定合法并且存储数据
				String ticket = managerService.doLogin(manager);
				// 控制层将业务层存储登录成功的rediskey值
				// !"".equals(ticket)&&ticket!=null
				if (StringUtils.isNotEmpty(ticket)) {//
					// ticket非空,redis存好登录的查询结果
					// 将ticket作为cookie的值返回 cookie
					// 的名称,调用Cookie工具类 //添加到cookie返回给前端
					CookieUtils.setCookie(request, response, "ADMIN_TICKET", ticket);
					return SysResult.ok();
				} else {
					// 什么都不做直接返回失败
					return SysResult.build(201, "SHIBAI", null);
				}

	}
	@RequestMapping("/queryManager/{ticket}")
	public SysResult checkLoginManager(@PathVariable String ticket) {
		String managerJson = managerService.queryManagerJson(ticket);
		// 判断非空
		if (StringUtils.isNotEmpty(managerJson)) {
			// 确实曾经登录过.也正在登录使用状态中
			return SysResult.build(200, "ok", managerJson);
		} else {
			return SysResult.build(201, "", null);
		}
	}
	/**
	 * @return 管理员后台注销
	 */
	@RequestMapping("managerLogout")
	public void managerLogout(Manager manager) {
		managerService.logout(manager);
	}


	/**
	 * @return 管理员修改密码
	 */
	@RequestMapping
	public void modifyManagerPassword(Manager manager) {
		managerService.modifyManagerPassword(manager);
	}
}
